<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.js"></script>
            <script type="text/javascript" src="script/jquery.tablesorter.pager.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    // call the tablesorter plugin
                    $("table").tablesorter({
                        // sort on the first column and third column, order asc
                        sortList: [[1,0]],
                        headers: {
                            // assign the secound column (we start counting zero)
                            0: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            },
                            6: {
                                // disable it by setting the property sorter to false
                                sorter: false
                            }
                        }
                        ,
                        widthFixed: true, widgets: ['zebra']
                    })
                    .tablesorterPager({container: $("#pager")});
                    ;
                });

                function confirmDelete()
                {
                    var agree=confirm("Are you sure you want to deactivate user?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
            </script>

            <title>TUP User Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">

                        <a href="UMMViewUser.php"><b>[ User Management ]</b></a>&nbsp;&nbsp;

                    </div>

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="UMMAddUser.php" >Add User</a>&nbsp;
                        <a href="UMMViewUser.php">[ View User ]</a>&nbsp;
                        <a href="UMMSearchUser.php">Search User</a>&nbsp;
                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="AdminEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>

                <div align="center" style="  float:left; height:auto; width: 100%;">

                    <h2>View User</h2>
                    <form  method="post" action="userlist.php">
                        <input name="Export" class="submit" type="submit" value="Export"/>
                    </form>
                <?php
                if (isset($_POST['Cancel'])) {
                    $regidno = $_POST['idno']; //prof change to reg
                    echo "<h3>" . $regidno . "</h3>";
                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';
                    mysql_select_db($dbname);

                    // $query5 = "DELETE FROM user WHERE regidno='" . $regidno . "'";
                    $query5 = "update user set validate='deactivated' WHERE idno='" . $regidno . "'"; //prof change to reg

                    $queryvalue = mysql_query($query5);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Deactivate registrar failed</h3>"; //assumed registrar is the user being deactivated
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Registrar has been deactivated</h3>"; //asummed registrar is the user being deactivated
 include 'DBLibrary/closeDB.php';
                }
                ?>
                <?php
                include 'DBLibrary/config.php';
                include 'DBLibrary/openDB.php';

                mysql_select_db($dbname);
                $queryreg = "select * from user where validate='activated' and accounttype='registrar' order by idno"; //prof to reg
                $allreg = mysql_query($queryreg); //change to reg
                $ctr = 0;
                $numrows = mysql_num_rows($allreg); //change to reg

                echo "<aa>" . $numrows . " total results</aa>";
                ?>
                <table  border="0" id="myTable" class="tablesorter" style="width: 95%;" align="center">
                    <thead><tr>
                            <th>No</th>
                            <th>Registrar ID</th>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Gender</th>
                            <th>Email Address</th>
                            <th>Deactivate</th>
                        </tr></thead>
                    <tfoot><tr>
                            <th>No</th>
                            <th>Registrar ID</th>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Gender</th>
                            <th>Email Address</th>
                            <th>Deactivate</th>
                        </tr></tfoot>
                    <tbody>

                        <?php
                        while ($row = mysql_fetch_array($allreg)) {
                            $ctr++;
                            //if()
                            //echo "<tr>" . "<td>" . $ctr . "</td>";<th>No.</th>
                            echo "<tr><td>" . $ctr . "</td><td>" . $row['idno'] . "</td>"; //rollback reg to prof
                            echo "<td>" . $row['lname'] . "</td>";
                            echo "<td>" . $row['fname'] . "&nbsp;" . $row['mname'] . ".</td>";
                            echo "<td>" . $row['gender'] . "</td>";
                            echo "<td>" . $row['email'] . "</td>";
                            echo "<form onsubmit=\"return confirmDelete()\" name=\"add\" method=\"post\" action=\"\">";
                            echo "<td><input type=\"text\" name=\"idno\" size=\"1\" value=\"" . $row['idno'] . "\" style=\"visibility:hidden;\"/><input type=\"submit\" name=\"Cancel\" value=\"Deactivate\"/></form></td></tr>";
                        }
                        include 'DBLibrary/closeDB.php';
                        ?> </tbody>
                </table>

                <div id="pager" class="pager" align="center" style="float:left; height: auto; width: 100%;">
                    <form>
                        <img src="addons/pager/icons/first.png" class="first"/>
                        <img src="addons/pager/icons/prev.png" class="prev"/>
                        <input readonly="readonly" type="text" class="pagedisplay"/>
                        <img src="addons/pager/icons/next.png" class="next"/>
                        <img src="addons/pager/icons/last.png" class="last"/>
                        <select class="pagesize">
                            <option selected="selected"  value="10">10</option>

                            <option value="20">20</option>
                            <option value="30">30</option>
                            <option  value="40">40</option>
                        </select>
                    </form>
                    <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
                        <div align="center" style=" height: 4px; width: auto;"></div>
                        <bb class ="small">Copyright &#169; 2011</bb>
                    </div>
                </div>
                <br>

            </div>
        </div>

    </body>
</html>
<?php
                    } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
                        echo "<script language='javascript'>window.location='SMMViewStud.php'</script>";
                    } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                        echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
                    }
?>